<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['class:class:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['class:class:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['class:class:remove']"
        >删除</el-button>

      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="classList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="培训班期次" align="center" prop="classesTerm" />
      <el-table-column label="培训班课时" align="center" prop="trainPeriod" />
      <el-table-column label="培训班通知文号" align="center" prop="notificationNumber" />
      <el-table-column label="课程表(附件)" align="center" prop="urlClassTable" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.urlClassTable" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="报名册(附件)" align="center" prop="urlApplication" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.urlApplication" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="300px">
        <template slot-scope="scope">
          <el-button
          v-if="scope.row.isEnd == 0"
          style="width: 60px;"
            size="mini"
            type="success"
            @click="addStudent(scope.row)"
            v-hasPermi="['class:class:edit']"
          >添加学员</el-button>

          <el-button
          v-if="scope.row.isEnd == 0"
            size="mini"
            style="width: 40px;"
            type="warning"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['class:class:edit']"
          >修改</el-button>
          <el-button
          v-if="scope.row.isEnd == 0"
          style="width: 40px;"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
            v-hasPermi="['class:class:remove']"
          >删除</el-button>

          <el-button
          v-if="scope.row.isEnd == 0"
          style="margin-top: 10px; width: 60px;"
            size="mini"
            
            type="primary"
            @click="trainOver(scope.row)"
            v-hasPermi="['class:class:remove']"
          >培训结束</el-button>

          <el-button
          v-if="scope.row.isEnd == 1"
          key="autoButton"
          disabled  
            size="mini"
            type="text"
            @click="trainOver(scope.row)"
            v-hasPermi="['class:class:remove']"
          >培训已结束</el-button>
        </template>

        
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改培训班级对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form :inline="true" ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="培训班期次" prop="classesTerm">
          <el-input v-model="form.classesTerm" placeholder="请输入培训班期次" />
        </el-form-item>
        <el-form-item label="培训课时" prop="trainPeriod">
          <el-input v-model="form.trainPeriod" placeholder="请输入培训课时" />
        </el-form-item>
        <el-form-item label="培训班通知文号" prop="notificationNumber">
          <el-input v-model="form.notificationNumber" placeholder="请输入培训班通知文号" />
        </el-form-item>
        <el-form-item label="课程表(附件)" prop="urlClassTable">
          <image-upload v-model="form.urlClassTable"/>
        </el-form-item>
        <el-form-item label="报名册(附件)" prop="urlApplication">
          <image-upload v-model="form.urlApplication"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUrlParam } from "../../../api/utils";
import { listClass, getClass, delClass, addClass, updateClass, trainEnd } from "@/api/class/class";

export default {
  name: "Class",
  dicts: ['train_place','exam_place'],
  data() {
    return {
      // 培训计划id
      trainId: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训班级表格数据
      classList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trainName: null,
        examplace: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 培训结束按钮
    trainOver(row) {
      const ids = row.id || this.ids;
      const trainId = this.trainId;
      window.location.href = "../class/information?classId=" + ids + "&" + "isEnd=" + 1 + "&trainId=" + trainId;
    },
    // 添加学员
    addStudent(row) {
      const trainId = this.trainId;
      const ids = row.id || this.ids;
      window.location.href = "../class/information?classId=" + ids + "&" + "isEnd=" + row.isEnd + "&trainId=" + trainId;
    },
    /** 查询培训班级列表 */
    getList() {
      var id = getUrlParam("trainId")
      this.trainId = id;

      this.loading = true;
      listClass(id).then(response => {
      
        this.classList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        trainPlanId: null,
        notificationNumber: null,
        classesTerm: null,
        classesName: null,
        trainPeriod: null,
        urlClassTable: null,
        urlApplication: null,
        urlSign: null,
        urlInvalue: null,
        urlCertificate: null,
        urlFeeCode: null,
        trainName: null,
        examplace: null,
        createId: null,
        udpateId: null,
        createTime: null,
        updateTime: null,
        extend: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加培训班级";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getClass(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改培训班级";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.form.trainPlanId = this.trainId;
        if (valid) {
          if (this.form.id != null) {
            updateClass(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClass(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除培训班级编号为"' + ids + '"的数据项？').then(function() {
        return delClass(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('class/class/export', {
        ...this.queryParams
      }, `class_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
